package com.z6school.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.z6school.manage.common.annotation.RequiredLog;
import com.z6school.manage.common.exception.ServiceException;
import com.z6school.manage.common.pojo.PageObject;
import com.z6school.manage.mapper.StudentMapper;
import com.z6school.manage.po.Student;
import com.z6school.manage.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    @RequiredLog(operation = "删除学生信息")
    public int doDeleteObjects(Integer[] ids) {
        List<Integer> idList= Arrays.asList(ids);
        return studentMapper.deleteBatchIds(idList);
    }


    @Override
    @RequiredLog(operation = "查询学生信息")
    @RequiresPermissions("sys:student:view")
    public PageObject<Student> findPageObjects(String studentName, Integer pageCurrent) {
        //检验controller层传过来的参数,用户名,页码
        //1.验证页码
        if(pageCurrent<1 || pageCurrent==null) {
            throw new IllegalArgumentException("页码值不正确"); //参数不正确
        }
        //2.记录总数,根据传过来的用户名进行查询,传了用户名就是查询指定,没传即为null,即查询所有
        long rowCount = studentMapper.getCount(studentName);
        if(rowCount==0) {
            throw new ServiceException("查无记录");
        }
        //3.自定义页面大小
        int pageSize=5;

        //3.2计算当前页其实位置
        int startIndex=(pageCurrent-1)*pageSize;
        //4.计算总页数,在构造方法里计算就好
        List<Student> records = studentMapper.findObjects(studentName, startIndex, pageSize);
        return new PageObject<>(rowCount,records,pageSize,pageCurrent);
    }

    @Override
    @RequiredLog(operation = "新增学生信息")
    @RequiresPermissions("sys:student:edit")
    public void addStudentMessage(Student student) {
        studentMapper.insert(student);
    }

}
